package com.cw.jeeyt.service.impl.api;

import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cw.jeeyt.common.Order;
import com.cw.jeeyt.service.bo.api.ApiTableAbbreviationBo;
import com.cw.jeeyt.service.bo.security.SecurityUserBo;
import com.cw.jeeyt.core.util.SessionUtil;
import com.cw.jeeyt.dao.intf.api.ApiTableAbbreviationDao;
import com.cw.jeeyt.dao.model.api.ApiTableAbbreviationDo;
import com.cw.jeeyt.service.intf.api.ApiTableAbbreviationService;
import com.cw.jeeyt.service.converter.api.ApiTableAbbreviationConverter;
import com.cw.lang.common.utils.IdGenerator;
import com.cw.lang.mybatis.dto.PageInfo;

/**
 * 数据库表缩写配置业务实现类
 *
 * @author chenw
 * @date 2019-04-17 13:17:14
 */
@Service
public class ApiTableAbbreviationServiceImpl implements ApiTableAbbreviationService {

    private static final Logger logger = LoggerFactory.getLogger(ApiTableAbbreviationServiceImpl.class);
    @Autowired
    private ApiTableAbbreviationDao apiTableAbbreviationDao;//数据库表缩写配置数据库操作接口

    /**
     * 查询所有数据库表缩写配置
     */
    @Override
    public List<ApiTableAbbreviationBo> queryApiTableAbbreviationList() throws Exception {
        return ApiTableAbbreviationConverter.doList2BoList(apiTableAbbreviationDao.queryApiTableAbbreviationList());
    }

    /**
     * 分页查询数据库表缩写配置
     *
     * @param apiTableAbbreviationBo
     * @param startNum
     * @param pageSize
     * @return
     * @throws Exception
     */
    @Override
    public PageInfo<ApiTableAbbreviationBo> queryByPage(ApiTableAbbreviationBo apiTableAbbreviationBo, Integer startNum,
        Integer pageSize, Order order) throws Exception {

        if (apiTableAbbreviationBo == null) {
            logger.error("数据库表缩写配置为空");
            return null;
        }
        PageInfo<ApiTableAbbreviationDo> pageInfo = apiTableAbbreviationDao.queryByPage(
            ApiTableAbbreviationConverter.bo2Do(apiTableAbbreviationBo), startNum, pageSize, order);
        return new PageInfo<>(pageInfo, ApiTableAbbreviationConverter.doList2BoList(pageInfo.getList()));
    }

    /**
     * 根据主键查询数据库表缩写配置信息
     *
     * @return
     */
    @Override
    public ApiTableAbbreviationBo getApiTableAbbreviationById(Long id) throws Exception {

        if (null == id) {
            logger.error("--主键id为空--");
            return null;
        }
        return ApiTableAbbreviationConverter.do2Bo(apiTableAbbreviationDao.getById(id));
    }

    /**
     * 新增数据库表缩写配置
     *
     * @param apiTableAbbreviationBo
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer insert(ApiTableAbbreviationBo apiTableAbbreviationBo) throws Exception {

        if (apiTableAbbreviationBo == null) {
            logger.error("数据库表缩写配置为空");
            return null;
        }
        SecurityUserBo userBo = SessionUtil.getSessionUser();
        if (userBo == null) {
            logger.error("用户未登录");
            throw new Exception("用户未登录");
        }
        ApiTableAbbreviationDo apiTableAbbreviation = ApiTableAbbreviationConverter.bo2Do(apiTableAbbreviationBo);
        apiTableAbbreviation.setCreateBy(userBo.getId());
        apiTableAbbreviation.setCreateDate(new Date());
        apiTableAbbreviation.setUpdateBy(userBo.getId());
        apiTableAbbreviation.setUpdateDate(new Date());
        apiTableAbbreviation.setId(IdGenerator.nextId());
        Integer result = apiTableAbbreviationDao.insert(apiTableAbbreviation);
        return result;
    }

    /**
     * 新增数据库表缩写配置不为空的字段
     *
     * @param apiTableAbbreviationBo
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer insertSelective(ApiTableAbbreviationBo apiTableAbbreviationBo) throws Exception {

        if (apiTableAbbreviationBo == null) {
            logger.error("数据库表缩写配置为空");
            return null;
        }
        SecurityUserBo userBo = SessionUtil.getSessionUser();
        if (userBo == null) {
            logger.error("用户未登录");
            throw new Exception("用户未登录");
        }
        ApiTableAbbreviationDo apiTableAbbreviation = ApiTableAbbreviationConverter.bo2Do(apiTableAbbreviationBo);
        apiTableAbbreviation.setCreateBy(userBo.getId());
        apiTableAbbreviation.setCreateDate(new Date());
        apiTableAbbreviation.setUpdateBy(userBo.getId());
        apiTableAbbreviation.setUpdateDate(new Date());
        apiTableAbbreviation.setId(IdGenerator.nextId());
        Integer result = apiTableAbbreviationDao.insertSelective(apiTableAbbreviation);
        return result;
    }

    /**
     * 更新数据库表缩写配置
     *
     * @param apiTableAbbreviationBo
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(ApiTableAbbreviationBo apiTableAbbreviationBo) throws Exception {

        if (apiTableAbbreviationBo == null) {
            logger.error("数据库表缩写配置为空");
            return;
        }
        SecurityUserBo userBo = SessionUtil.getSessionUser();
        if (userBo == null) {
            logger.error("用户未登录");
            throw new Exception("用户未登录");
        }
        ApiTableAbbreviationDo apiTableAbbreviation = ApiTableAbbreviationConverter.bo2Do(apiTableAbbreviationBo);
        apiTableAbbreviation.setUpdateBy(userBo.getId());
        apiTableAbbreviation.setUpdateDate(new Date());

        apiTableAbbreviationDao.update(apiTableAbbreviation);
    }

    /**
     * 更新数据库表缩写配置不为空的字段
     *
     * @param apiTableAbbreviationBo
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateSelective(ApiTableAbbreviationBo apiTableAbbreviationBo) throws Exception {

        if (apiTableAbbreviationBo == null) {
            logger.error("数据库表缩写配置为空");
            return;
        }
        SecurityUserBo userBo = SessionUtil.getSessionUser();
        if (userBo == null) {
            logger.error("用户未登录");
            throw new Exception("用户未登录");
        }
        ApiTableAbbreviationDo apiTableAbbreviation = ApiTableAbbreviationConverter.bo2Do(apiTableAbbreviationBo);
        apiTableAbbreviation.setUpdateBy(userBo.getId());
        apiTableAbbreviation.setUpdateDate(new Date());

        apiTableAbbreviationDao.updateSelective(apiTableAbbreviation);
    }

    /**
     * 删除数据库表缩写配置
     *
     * @param apiTableAbbreviationBo
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(ApiTableAbbreviationBo apiTableAbbreviationBo) throws Exception {

        if (apiTableAbbreviationBo == null) {
            logger.error("数据库表缩写配置为空");
            return;
        }
        ApiTableAbbreviationDo apiTableAbbreviation = ApiTableAbbreviationConverter.bo2Do(apiTableAbbreviationBo);
        apiTableAbbreviationDao.delete(apiTableAbbreviation);
    }
}
